package com.example.xgakj.newterminal.ui.activities;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xgakj.mylibrary.mvp.utils.SharedPreferencesUtil;
import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.adapter.BellChoiceAdapter;
import com.example.xgakj.newterminal.base.BaseActivity;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.utils.SharedPreferencesManager;
import com.example.xgakj.newterminal.utils.SystemDataManager;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ClassName:SettingBellRingActivity
 * @Description:
 * @author Administrator
 * @date 2015-8-19 下午6:49:51
 *
 */
public class SettingBellRingActivity extends BaseActivity implements OnClickListener {
	BellChoiceAdapter adapter;
	private ImageButton button_back;
	private ListView list_sys_bell;
	private Button button_save;
	int checkedId;
	int selectPos;
	int bellType;
	MediaPlayer player;
	private TextView selectBell;
	MyReceiver receiver;
	boolean myreceiverIsReg = false;
	private RelativeLayout set_sd_bell_btn;
	private QMUITopBar topbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_bell_ring);
		initTopbar();
		init();
		initListener();
		registerMonitor();
		initSelectMusicName();
	}

	private void initTopbar() {
		topbar = (QMUITopBar) findViewById(R.id.topbar);
		topbar.setTitle("铃声设置");
		topbar.addLeftBackImageButton().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		topbar.addRightTextButton("保存",QMUIViewHelper.generateViewId()).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(checkedId==-1){
				 Toast.makeText(SettingBellRingActivity.this, "未选择铃声", Toast.LENGTH_SHORT).show();
			 }else{
				SharedPreferencesManager.getInstance().putASystemBellId(checkedId,mContex );
				SharedPreferencesManager.getInstance().putABellSelectPos(selectPos, mContex);
				 String path= SystemDataManager.getInstance().findSystemBellById(getApplicationContext(),checkedId).get("path");
					SharedPreferencesUtil.setString(mContex, Parameters.MUSIC,path);
				SharedPreferencesManager.getInstance().putABellType(SharedPreferencesManager.TYPE_BELL_SYS, mContex);
				Intent i = new Intent();
				i.setAction(Parameters.ACTION_CHANGEBELL);
				sendBroadcast(i);
				SettingBellRingActivity.this.finish();
			 }
			}
		});
	}

	private void init() {
		player = new MediaPlayer();

		set_sd_bell_btn = (RelativeLayout) findViewById(R.id.set_sd_bell_btn);
		selectBell = (TextView) findViewById(R.id.selectBell);
		list_sys_bell = (ListView) findViewById(R.id.list_sys_bell);
		initSelectState();
		ArrayList<HashMap<String,String>> bells = SystemDataManager.getInstance().getSysBells(SettingBellRingActivity.this);
		adapter = new BellChoiceAdapter(SettingBellRingActivity.this, bells);
		list_sys_bell.setAdapter(adapter);
		adapter.setCheckedId(checkedId);
		list_sys_bell.setSelection(selectPos);
	}
	
	public void registerMonitor(){
		IntentFilter filter = new IntentFilter();
		filter.addAction(Parameters.ACTION_CHANGEBELL);
		receiver = new MyReceiver();
		this.registerReceiver(receiver, filter);
		myreceiverIsReg = true;
	}
	
	private void initListener() {
		set_sd_bell_btn.setOnClickListener(this);
		list_sys_bell.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				HashMap<String,String> data = (HashMap<String, String>) adapter.getItem(arg2);
				int id = Integer.parseInt(data.get("bellId"));
				checkedId = id;
				selectPos = arg2;
				adapter.setCheckedId(id);
				adapter.notifyDataSetChanged();
				playMusic(checkedId);
			}
		});
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.set_sd_bell_btn:
			startActivity(new Intent(SettingBellRingActivity.this, SettingSdBellActivity.class));
			break;

		default:
			break;
		}
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(myreceiverIsReg){
			this.unregisterReceiver(receiver);
		}
		player.stop();
		player.release();
	}
	
	public void playMusic(int bellId){
		
		try {
			player.reset();
			HashMap<String,String> data;
			data = SystemDataManager.getInstance().findSystemBellById(SettingBellRingActivity.this, bellId);
			String path = data.get("path");

			if(null==path||"".equals(path)){
				
			}else{
				player.setDataSource(path);
				player.prepare();
				player.start();
			}
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void initSelectState(){

			selectPos = SharedPreferencesManager.getInstance().getABellSelectPos(this);
			bellType = SharedPreferencesManager.getInstance().getABellType(this);
			
			if(bellType==SharedPreferencesManager.TYPE_BELL_SYS){
				checkedId = SharedPreferencesManager.getInstance().getASystemBellId(this);
				selectBell.setText("");
			}else{
				checkedId = SharedPreferencesManager.getInstance().getASdBellId(this);
				HashMap<String,String> data = SystemDataManager.getInstance().findSdBellById(SettingBellRingActivity.this, checkedId);
				if(null!=data){
					selectBell.setText(data.get("bellName"));
				}
				checkedId = -1;
				selectPos = 1;
			}
		
	}
	
	public class MyReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context arg0, Intent intent) {
			// TODO Auto-generated method stub
			if(intent.getAction().equals(Parameters.ACTION_CHANGEBELL)){
				initSelectMusicName();
				initSelectState();
				list_sys_bell.setSelection(selectPos);
				adapter.setCheckedId(checkedId);
				adapter.notifyDataSetChanged();
			}
			
		}
	}
	
	private void initSelectMusicName() {
		 int abellType = SharedPreferencesManager.getInstance().getABellType(this);
			if(abellType==SharedPreferencesManager.TYPE_BELL_SYS){
				int bellId = SharedPreferencesManager.getInstance().getASystemBellId(this);
				HashMap<String,String> data = SystemDataManager.getInstance().findSystemBellById(this, bellId);
				if(null!=data){
					selectBell.setText("");
				}
			}else{
				int bellId = SharedPreferencesManager.getInstance().getASdBellId(this);
				HashMap<String,String> data = SystemDataManager.getInstance().findSdBellById(this, bellId);
				if(null!=data){
					selectBell.setText(data.get("bellName"));
				}
			}
		
	}

}
